<template>
    <div class="shop-edit">
        <!-- Drawer抽屉 -->
        <Drawer
                :title="title"
                v-model="visible"
                width="720"
                draggable
                :mask-closable="type == '0'">
            <div :style="{ maxHeight: maxHeight }" class="drawer-content">
                <div class="drawer-header">
                    <div style="margin-right: 16px">基本信息</div>
                </div>
                <Form label-colon v-show="type != '2'">
                    <Row :gutter="32">
                        <Col span="12">
                            <FormItem label="设备ID">
                                {{ form.id }}
                                <Tooltip trigger="hover" placement="right" content="设备已禁用">
                                    <Icon
                                            v-show="form.status == -1"
                                            type="md-lock"
                                            size="18"
                                            style="margin-left: 10px; cursor: pointer"
                                    />
                                </Tooltip>
                            </FormItem>
                        </Col>
                        <Col span="12">
                            <FormItem label="设备名称">
                                {{ form.name }}
                            </FormItem>
                        </Col>
                    </Row>
                </Form>
                <Form
                        ref="form"
                        :model="form"
                        :rules="formValidate"
                        label-position="top">
                    <Row :gutter="32">
                        <Col span="12">
                            <FormItem label="设备MAC地址" prop="mac">
                                {{form.mac}}
                            </FormItem>
                        </Col>
                        <Col span="12">
                            <FormItem label="设备地址" prop="address">
                                {{form.address}}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row :gutter="32">
                        <Col span="12">
                            <FormItem label="设备类型" prop="type">
                                <Input v-model="form.type"/>
                            </FormItem>
                        </Col>
                        <Col span="12">
                            <FormItem label="所属店铺">
                                <Input v-model="form.shopId"/>
                            </FormItem>
                        </Col>
                    </Row>
                    <Divider/>
                    <p class="drawer-title">店铺资料</p>
                    <Row :gutter="32">
                        <Col span="24">
                            <FormItem label="备注">
                                <Input type="textarea" v-model="form.description" :rows="4"/>
                            </FormItem>
                        </Col>
                    </Row>
                </Form>
            </div>
            <div class="drawer-footer br" v-show="type != '0'">
                <Button type="primary" :loading="submitLoading" @click="submit">提交
                </Button>
                <Button @click="visible = false">取消</Button>
            </div>
        </Drawer>
    </div>
</template>

<script>
    import {validateMobile,} from "@/libs/validate";
    import {addDevice, updateDevice} from "@/api/things";
    import uploadPicInput from "@/views/my-components/mrdeer/upload-pic-input";

    export default {
        name: "device",
        components: {
            uploadPicInput
        },
        props: {
            value: {
                type: Boolean,
                default: false,
            },
            data: {
                type: Object,
            },
            type: {
                type: String,
                default: "0",
            },
        },
        data() {
            return {
                visible: this.value,
                title: "",
                submitLoading: false,
                maxHeight: 510,
                form: {
                    address: [],
                },
                formValidate: {
                    // 表单验证规则

                    phone: [
                        {required: true, message: "请输入手机号", trigger: "change"},
                        {validator: validateMobile, trigger: "change"},
                    ],
                    email: [
                        {required: true, message: "请输入邮箱地址"},
                        {type: "email", message: "邮箱格式不正确"},
                    ]
                },
            };
        },
        methods: {
            init() {

            },
            submit() {
                this.$refs.form.validate((valid) => {
                    if (valid) {
                        if (this.type == "1") {
                            // 编辑
                            this.submitLoading = true;
                            updateDevice(this.form).then((res) => {
                                this.submitLoading = false;
                                if (res.status == 200) {
                                    this.$Message.success("操作成功");
                                    this.$emit("on-submit", true);
                                    this.visible = false;
                                }
                            });
                        } else {
                            // 添加
                            this.submitLoading = true;
                            addDevice(this.form).then((res) => {
                                this.submitLoading = false;
                                if (res.status == 200) {
                                    this.$Message.success("操作成功");
                                    this.$emit("on-submit", true);
                                    this.visible = false;
                                }
                            });
                        }
                    }
                });
            },
            setCurrentValue(value) {
                if (value === this.visible) {
                    return;
                }
                if (this.type == "1") {
                    this.title = "编辑设备";
                    this.maxHeight =
                        Number(document.documentElement.clientHeight - 121) + "px";
                } else if (this.type == "2") {
                    this.title = "添加设备";
                    this.maxHeight =
                        Number(document.documentElement.clientHeight - 121) + "px";
                } else {
                    this.title = "设备详情";
                    this.maxHeight = "100%";
                }
                // 清空数据
                this.$refs.form.resetFields();
                if (this.type == "0" || this.type == "1") {
                    // 回显数据
                    let data = this.data;
                    this.form = data;
                } else {
                    // 添加
                    this.form = {
                        type: 0,
                        address: []
                    };
                }
                this.visible = value;
            },
        },
        watch: {
            value(val) {
                this.setCurrentValue(val);
            },
            visible(value) {
                this.$emit("input", value);
            },
        },
        mounted() {
            this.init();
        },
    };
</script>

<style lang="less">
    @import "../../../styles/drawer-common.less";
</style>

